package com.lidong.zc.databasemodel;

import com.lidong.zc.databasemodel.entity.database.DataBaseTable;
import com.lidong.zc.databasemodel.init.intf.InitDataBase;
import com.lidong.zc.databasemodel.utils.LowcodeDbSpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据库自动配置启动类
 *
 * @author LiDong
 * @version 1.0.0
 * @createTime 9/9/2022 8:15 PM
 */
@Component
@Order(Integer.MIN_VALUE)
public class InitDataBaseStarter implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(InitDataBaseStarter.class);

    /**
     * 数据库自动配置建表入口
     *
     * @param args args
     * @throws Exception exception
     */
    @Override
    public final void run(String... args) throws Exception {
        logger.info("lowcode-database 建表开始...");
        // 创建数据库建表记录表
        InitDataBase initDataBase = LowcodeDbSpringContextUtils.getBean(InitDataBase.class);
        // 创建数据库建表固化表
        initDataBase.createCuringTable();
        List<DataBaseTable> dataBaseTableList = initDataBase.scanAnnotationGetTableInfo();
        logger.info("lowcode-database 共扫描到表 {} 个", dataBaseTableList.size());
        initDataBase.start(dataBaseTableList);
        logger.info("lowcode-database 建表结束...");
    }
}
